﻿using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Text;
using System.Web.Mvc;

namespace BBICMS.Model.Validation
{
    public class ZipCodeAttribute    : RegularExpressionAttribute, IClientValidatable
    {
        public ZipCodeAttribute()
            : base(@"[1-9]d{5}(?!d)")
        {
        }

        public IEnumerable<ModelClientValidationRule> GetClientValidationRules(ModelMetadata metadata, ControllerContext context)
        {
            var errorMessage = FormatErrorMessage(metadata.GetDisplayName());

            yield return new ZipCodeValidationRule(errorMessage);
        }
    }
    public class ZipCodeValidationRule : ModelClientValidationRule
    {
        public ZipCodeValidationRule(string errorMessage)
        {
            ErrorMessage = errorMessage;
            ValidationType = "zipcode";
        }
    }
}